export function getDateDisplay(date, {short=true, ellip=true, now=true}={}) {
  if (typeof(date)==='number'){
    date=decodeTimestamp(date);
  }
  let nowDate = new Date();
  let dateStr;
  if (now){
    dateStr = getDateDisplayFromNow(date, nowDate);
  }
  if (!dateStr){
    let dateData = decodeDate(date, nowDate, ellip);
    dateStr = buildDateStr(dateData, short);
  }
  return dateStr;
}

function getDateDisplayFromNow(date, now){
  let delta = (now.getTime()-date.getTime())/1000;
  if (delta >= 0){
    if (delta<=60){
      return '刚刚';
    } else if (delta<60*60){
      return parseInt(delta/60)+'分钟前';
    } else if (delta<=3*60*60){
      return parseInt(delta/60/60)+'小时前';
    }
  }
}

function decodeDate(date, now, ellip){
  let result = {};
  let nY=now.getFullYear();
  let nMon = now.getMonth() + 1;
  let nD = now.getDate(); 
  let y=date.getFullYear();
  let mon = date.getMonth() + 1;
  let d = date.getDate();
  let m = date.getMinutes();
  let h = date.getHours();
  if (!ellip || nY!==y){
    result.y=y;
  }
  if (!ellip || nY!==y || nMon!==mon || nD!==d){
    result.mon=mon;
    result.d = d;
  }
  result.m = m;
  result.h = h;
  return result;
}

function buildDateStr(dateData, short){
  let str='';
  if (dateData.y!==undefined){
    if (short){
      str += dateData.y+'/';
    } else {
      str += dateData.y+'年';
    }
  }
  if (dateData.mon!==undefined){
    if (short){
      str += dateData.mon+'/';
    } else {
      str += dateData.mon+'月';
    }
  }
  if (dateData.d!==undefined){
    if (short){
      str += dateData.d;
    } else {
      str += dateData.d+'日';
    }
  }
  if (str){
    str+=' ';
  }
  if (!short){
    if (dateData.h > 12){
      str+='下午 '+(dateData.h-12);
    } else {
      str+='上午 '+dateData.h;
    }
  } else {
    str +=dateData.h;
  }
  if (dateData.m<10){
    str+=':0'+dateData.m
  } else {
    str+=':'+dateData.m
  }
  return str;
}

export function encodeTimestamp(date) {
  return parseInt(date.getTime()/1000);
}

export function decodeTimestamp(timestamp) {
  return new Date(timestamp*1000);
}

export function getLastDatetime(datetime, duration) {
  if (duration.unit === 'm') {
    return new Date(datetime.getTime() - duration.value*60*1000);
  } else if (duration.unit === 'h') {
    return new Date(datetime.getTime() - duration.value*60*60*1000);
  } else if (duration.unit === 'd') {
    return new Date(datetime.getTime() - duration.value*24*60*60*1000);
  } else if (duration.unit === 'M') {
    let newDatetime = new Date(datetime.getTime());
    let yValue = parseInt(duration.value/12);
    let mValue = duration.value%12;
    let m = datetime.getMonth();
    if (m>=mValue) {
      newDatetime.setFullYear(datetime.getFullYear() - yValue);
      newDatetime.setMonth(m-mValue);
    } else {
      newDatetime.setFullYear(datetime.getFullYear() - yValue - 1);
      newDatetime.setMonth(m+12-mValue);
    }
    return newDatetime;
  } else if (duration.unit === 'y') {
    let newDatetime = new Date(datetime.getTime());
    newDatetime.setFullYear(datetime.getFullYear()-duration.value);
    return newDatetime;
  }
}